﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PHTShop.admin
{
    public partial class createCategory : System.Web.UI.Page
    {
        private DataContextDataContext dc = new DataContextDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "Create category";
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string catEn = CatName_en.Value.Trim();
            string catFr = CatName_fr.Value.Trim();
            string catGr= CatName_gr.Value.Trim();

            if (string.IsNullOrEmpty(catEn) && string.IsNullOrEmpty(catFr) && string.IsNullOrEmpty(catGr))
            {
                labelError.Text = "Category name can not be empty";
            }
            else {
                Category category = new Category { 
                status=true,
                orders=1,
                };
                dc.Categories.InsertOnSubmit(category);
                dc.SubmitChanges();
                if (!string.IsNullOrEmpty(catEn))
                {
                    insert(category, catEn, 1);
                }
                else {
                    insert(category, " ", 1);
                }
                if (!string.IsNullOrEmpty(catFr))
                {
                    insert(category, catFr, 2);
                }
                else
                {
                    insert(category, " ", 2);
                }
                if (!string.IsNullOrEmpty(catGr))
                {
                    insert(category, catGr, 3);
                }
                else {
                    insert(category, " ", 3);
                
                }

                Response.Redirect("categoryList.aspx");
            }
        }


        private void insert(Category cat,string name,int languageId) {

            CategoriesLanguage cl = new CategoriesLanguage
            {
                categoriesId = cat.id,
                name = name,
                languageId = languageId,
            };

            dc.CategoriesLanguages.InsertOnSubmit(cl);
            dc.SubmitChanges();
           
        
        }
    }
}